﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Medival_Snake
{

    public class Map
    {
        public int MapWidth;
        public int MapHeight;
        public int TileHeight;
        public int TileWidth;
        public static string fileName;

        CollisionBuilder MapCollision = new CollisionBuilder();
        WayPointBuilder MapWps = new WayPointBuilder();

        public List<Rectangle> Solids = new List<Rectangle>();
        public List<Rectangle> WayPointsUp = new List<Rectangle>();
        public List<Rectangle> WayPointsDown = new List<Rectangle>();
        public List<Rectangle> WayPointsLeft = new List<Rectangle>();
        public List<Rectangle> WayPointsRight = new List<Rectangle>();
        public List<Rectangle> WayPointsAllDir = new List<Rectangle>();

        public int[,] map;
        public int[,] wayPoints;

        public void MapLoader()
        {

            StreamReader s = new StreamReader(@fileName + ".txt");

            MapHeight = int.Parse(s.ReadLine());
            MapWidth = int.Parse(s.ReadLine());
            TileWidth = int.Parse(s.ReadLine());
            TileHeight = int.Parse(s.ReadLine());

            map = new int[MapHeight, MapWidth];
            wayPoints = new int[MapHeight, MapWidth];

            for (int i = 0; i < MapWidth; i++)
            {
                for (int j = 0; j < MapHeight; j++)
                {
                    map[j, i] = int.Parse(s.ReadLine());
                }
            }

            s.Close();
            s.Dispose();

        }

        public void LoadWpLayer()
        {
            try
            {
                StreamReader s = new StreamReader(@fileName + "Wp.txt");

                for (int i = 0; i < 25; i++)
                {
                    for (int j = 0; j < 15; j++)
                    {
                        wayPoints[j, i] = int.Parse(s.ReadLine());
                    }
                }

                s.Close();

            }
            catch
            {
                ;//empty
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Game1.Lvl1.WayPointsAllDir.Clear();
            Game1.Lvl1.WayPointsDown.Clear();
            Game1.Lvl1.WayPointsLeft.Clear();
            Game1.Lvl1.WayPointsRight.Clear();
            Game1.Lvl1.WayPointsUp.Clear();
            Solids.Clear();

            int tileMapWidth = map.GetLength(1);
            int tileMapHeight = map.GetLength(0);

            for (int x = 0; x < tileMapWidth; x++)
            {
                for (int y = 0; y < tileMapHeight; y++)
                {
                    int textureIndex = map[y, x];
                    int WpIndex = wayPoints[y, x];
                    Texture2D texture = Tiles.TileTexture[textureIndex];

                    spriteBatch.Draw( //draw map
                        texture,
                        new Rectangle(x * TileWidth,
                        y * TileHeight,
                        TileWidth, TileHeight),
                        Color.White);

                    MapCollision.CreateRect(textureIndex, x, y);
                    MapWps.CreateRect(WpIndex, x, y);
                    }
                }
            }
        }
}


